<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
namespace Agp\CoreBundle\Manager;

use Doctrine\ORM\EntityManager;
use Agp\CoreBundle\Entity\SolicitacaoRepasse;
use Agp\CoreBundle\Manager\BaseManager;
use Agp\AdminBundle\Form\Handler\SolicitacaoRepasseHandler;

/**use Agp\CoreBundle\Manager\BaseManager;
 * Description of SolicitacaoRepasseManager
 *
 * @author bondcs
 */
class SolicitacaoRepasseManager extends BaseManager
{
    public function persistSolicitacaoRepasse($entity, $estrategia) {
        parent::persist($entity);
        
        if ($estrategia == SolicitacaoRepasseHandler::INSERT)
            $this->container->get('agp.email')->emailRepassePendente($entity);
    }
    
    public function updateSolicitacaoRepasse($entity, $entityOld, $estrategia) {
        
        parent::update($entity);
        
        if ($estrategia == SolicitacaoRepasseHandler::UPDATE_AGAPE AND $entity->getSituacao() != $entityOld->getSituacao()){
            if ($entity->getSituacao() == SolicitacaoRepasse::SIT_PENDENTE){
                
                foreach ($entity->getPedidos() as $p){
                    $p->setSolicitacaoRepasse(null);
                    $p->setRepassado(false);
                    $this->em->persist($p);
                }
                
                foreach ($entity->getRepasses() as $r){
                    $this->em->remove($r);
                }
                
                $this->em->flush();
            }
        }
        
        parent::update($entity);
    }
    
    public function remove($entity) {
        
        foreach ($entity->getPedidos() as $p){
            $p->setSolicitacaoRepasse(null);
            $p->setRepassado(false);
            $this->em->persist($p);
        }
        
        $this->em->flush();
        
        parent::remove($entity);
    }

}

?>
